package com.xszx.controller;


import com.xszx.entity.TAsset;
import com.xszx.entity.dto.AssetCheck;
import com.xszx.entity.enums.AssetEnmu;
import com.xszx.entity.TDept;
import com.xszx.entity.TUser;
import com.xszx.service.TAssetService;
import com.xszx.service.TDeptService;
import com.xszx.util.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
public class AssetController {

    @Autowired
    private TAssetService assetService;

    @Autowired
    private TDeptService deptService;

    /**
     * 模糊查询，根据名称 编号  价值
     */
    @ResponseBody
    @RequestMapping("/selectAssetsByCheck")
//    acode:$("#checkid").val(),
//    aname:$("#checkname").val(),
//    min:$("#min").val(),
//    max:$("#max").val(),
    public List<TAsset> selectAssetsByCheck(HttpServletRequest request, TAsset asset){
        TUser user = (TUser) request.getSession().getAttribute("user");
        List<TAsset> assetList = new ArrayList<>();
        AssetCheck assetCheck = new AssetCheck();
        assetCheck.setAcode(asset.getAcode());
        assetCheck.setAprice(asset.getAprice());
        assetCheck.setAstatus(asset.getAstatus());
        assetCheck.setAname(asset.getAname());
        if(user.getDid()!=101){
            assetCheck.setDid(user.getDid());
            System.out.println(assetCheck);
            assetList = assetService.selectAssetsByCheck(assetCheck);
            log.info("模糊查询所有资产信息:{}",assetList);
        } else {
                assetList = assetService.selectAssetsByCheck(assetCheck);
                log.info("模糊查询所有资产信息:{}",assetList);
                log.info("模糊查询资产信息:{}",assetList);
        }

        System.out.println(assetList);
        return assetList;
    }



    /**
     * 获取资产状态比例
     */
    @ResponseBody
    @RequestMapping("/getAssectCount")
    public int[] getAssectCount(HttpServletRequest request){
        List<TAsset> assetList = new ArrayList<>();
            assetList = assetService.selectAssetLists();
            log.info("获取资产状态比例:{}",assetList);

            int s=0;
            int k=0;
        for (TAsset tAsset : assetList) {
            if(tAsset.getAstatus().equals("空闲")){
                s=s+1;
            }else{
                k=k+1;
            }
        }
        int sun = assetList.size();
         s = s*100/sun;
         k = k*100/sun;
         int[] z=new int[]{s,k};
         return z;

    }
    /**
     * 遍历资产表
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAssets")
    public List<TAsset> selectAssetList(HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        List<TAsset> assetList = new ArrayList<>();
        if(user.getDid()!=101){
            assetList = assetService.selectAssetList(user.getDid());
            log.info("遍历所有资产信息:{}",assetList);
        } else {
            assetList = assetService.selectAssetLists();
            log.info("遍历所有资产信息:{}",assetList);
        }

        System.out.println(assetList);
        return assetList;
    }
    //selectAssetsUsing
    /**
     * 遍历资产表（状态为使用中的）
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAssetsUsing")
    public List<TAsset> selectAssetsUsing(HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        List<TAsset> assetList = assetService.selectAssetListUsing(user.getDid());
        log.info("遍历所有状态为空闲和使用中的资产：{}",assetList);
        return assetList;
    }
    //selectAssetsFree
    /**
     * 遍历资产表（状态为空闲）
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAssetsFree")
    public List<TAsset> selectAssetsFree(HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        List<TAsset> assetList = assetService.selectAssetListFree(user.getDid());
        log.info("遍历所有状态为空闲的资产：{}",assetList);
        return assetList;
    }
    //selectAssetsUsed
    /**
     * 遍历资产表（状态为维修中）
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAssetsFix")
    public List<TAsset> selectAssetsFix(HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        List<TAsset> assetList = assetService.selectAssetListFix(user.getDid());
        log.info("遍历所有状态为维修中的资产：{}",assetList);
        return assetList;
    }

    //selectAssetUsed1
    /**
     * 遍历资产表（状态为禁用）
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAssetForbidden")
    public List<TAsset> selectAssetForbidden(HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        List<TAsset> assetList = assetService.selectAssetListForbidden(user.getDid());
        log.info("遍历所有状态为禁用的资产：{}",assetList);
        return assetList;
    }

    /**
     * 遍历资产表（状态为报废）
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAssetScrap")
    public List<TAsset> selectAssetScrap(HttpServletRequest request) {
        TUser user = (TUser) request.getSession().getAttribute("user");
        List<TAsset> assetList = assetService.selectAssetListScrap(user.getDid());
        log.info("遍历所有状态为禁用的资产：{}", assetList);
        return assetList;
    }

    /**
     * 获取所有资产价值
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAssetSale")
    public double selectAssetSale(){
        List<TAsset> assetList = assetService.selectAssetLists();
        double sal = 0;
        for (TAsset tAsset : assetList) {
            sal=sal+tAsset.getAprice();
        }
        return sal;
    }
//    @RequestMapping("/getUserCount")
//    @ResponseBody
//    public int getUserCount(){
//        return userService.selectUser().size();
//    }
    //@RequestParam("id")int id
    /**
     * 根据id删除asset
     * @param
     * @return
     */
    @RequestMapping("/deleteAssetById")
    public String deleteAssetById(@RequestParam("aid") long aid){
        log.info("根据aid删除资产：{}",aid);
        int i = assetService.deleteAssetById(aid);
        System.out.println(i > 0 ? "删除成功！" : "删除失败！");
        System.out.println("受影响的行数为：" + i);
        return "jsp/assetTable";
    }


    @RequestMapping("/addAsset")
    public String addAsset(@RequestParam("aname") String aname, @RequestParam("aprice") double aprice, @RequestParam("adepreciation") double adepreciation, @RequestParam("aintodept") String aintodept, @RequestParam("aserial") String aserial, @RequestParam("address") String address){
        //雪花算法生成资产编号
        TAsset asset = new TAsset();
        long nextId = new SnowFlakeUtil().getNextId();
        System.out.println("生成的aid:"+nextId);
        asset.setAid(nextId);
        asset.setAname(aname);
        asset.setAprice(aprice);
        asset.setAdepreciation(adepreciation);
        asset.setAintodept(aintodept);
        asset.setAintodate(new Timestamp(new Date().getTime()));
        TDept deptByName = deptService.getDeptByName(aintodept);
        asset.setDid(deptByName.getDid());
        asset.setAserial(aserial);
        asset.setAstatus("空闲");
        asset.setAaddress(address);
        long code = new SnowFlakeUtil().getNextId();
        asset.setAcode(code);
        log.info("添加资产：{}",asset);

        int i = assetService.addAsset(asset);
        System.out.println(i > 0 ? "添加成功！" : "添加失败！");
        System.out.println("受影响的行数为：" + i);
        return "jsp/assetTable";
    }
//@RequestBody TAsset asset
    @RequestMapping("/updateAsset")
    public String updateAsset(TAsset asset){
            log.info("修改资产：{}",asset);
        String aintodept = asset.getAintodept();
        TDept deptByName = deptService.getDeptByName(aintodept);
        asset.setDid(deptByName.getDid());
//        String name = AssetEnmu.getName(deptId).getName();
//        asset.setAstatus(name);

        int i = assetService.updateAsset(asset);
        System.out.println( i> 0 ? "修改成功！" : "修改失败！");
        System.out.println("受影响的行数为：" + i);
        return "jsp/assetTable";
    }

    @RequestMapping("/selectAssetByAid")
    @ResponseBody
    public TAsset selectAssetByAid(long aid,HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        log.info("根据aid查询资产：{}",aid);
        TAsset asset = assetService.getAssetByAid(aid,user.getDid());
       log.info("根据aid查询到的资产：{}",asset);
        return asset;
    }

    //selectStatusList
    @RequestMapping("/selectStatusList")
    @ResponseBody
    public String[] selectStatusList(){
        String[] status = new String[]{AssetEnmu.getName(1).getName(),AssetEnmu.getName(2).getName(),
        AssetEnmu.getName(3).getName(),AssetEnmu.getName(4).getName(),AssetEnmu.getName(5).getName()};
        log.info("遍历得到所有资产状态：{}",status);
        return status;
    }

}
